from selenium.webdriver import Chrome
import time
import sys
import os
d=os.getcwd()
sys.path.append(d)
driver=Chrome(executable_path="dist/chromedriver.exe")

#登陆超星尔雅(手机+短信验证码)
username="201631520866"
password="047410"
login_url="http://study.swpu.edu.cn/portal"
driver.get(login_url)
#点击校内登陆
driver.find_element_by_xpath("//div[@class='Loginbefore']/a[2]").click()
driver.find_element_by_name("username").send_keys(username)
driver.find_element_by_name("password").send_keys(password)
#点击登陆
driver.find_element_by_class_name("login").click()
#点击进入空间
driver.find_element_by_xpath("//div[@class='zaf_course']/p[@class='zaf_text']/a").click()
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(3)
frame=driver.switch_to.frame("frame_content")
a_list=driver.find_elements_by_xpath("//div[@class='Mcon1img httpsClass']/a[1]")
#遍历我学的课
for a in a_list:
    a.click()
    driver.switch_to.window(driver.window_handles[1])
    leveltwos=driver.find_elements_by_xpath("//div[@class='units']/div[@class='leveltwo']")

    while True:
        try:
            active=driver.find_element_by_xpath("//span[@class='icon']/em[@class='orange']/../following-sibling::span[1]/a")
            if active.text=="阅读":
                break
            else:
                active.click()
        except Exception as e:
            break
        time.sleep(2)
        test_elem=driver.find_element_by_xpath("//div[@class='tabtags']/span[@title='章节测验']")
        video_elem=driver.find_element_by_xpath("//div[@class='tabtags']/span[@title='视频']")
        test_elem.click()
        driver.switch_to.frame("iframe")
        driver.switch_to.frame(driver.find_element_by_xpath("//div[contains(@class,'ans-attach-ct')]/iframe"))
        driver.switch_to.frame("frame_content")
        #with open("test.html", "w", encoding="UTF8") as f:
        #    f.write(driver.page_source)

        ti_mu_list=driver.find_elements_by_xpath("//div[@class='TiMu']")
        try:
            driver.find_element_by_xpath("//h3/span[text()='已完成']")
        except Exception as e:
            for ti_mu in ti_mu_list:
                ti_mu_title = ti_mu.find_element_by_xpath(".//div[@class='Zy_TItle clearfix']/div").text
                input_list = ti_mu.find_elements_by_xpath(".//ul/li[1]/label/input")
                for input in input_list:
                    time.sleep(1)
                    input.click()
                time.sleep(2)
            # 暂时保存
            # driver.find_element_by_xpath("//div[@class='ZY_sub clearfix']/a[1]").click()
            # driver.switch_to.alert.accept()
            # 提交作业
            driver.find_element_by_xpath("//div[@class='ZY_sub clearfix']/a[2]").click()
            time.sleep(1)
            driver.find_element_by_xpath("//div[@class='AlertCon02']/div[@class='con03']/div/a[1]").click()
            time.sleep(2)
        else:
            driver.switch_to.default_content()
            video_elem.click()
            driver.switch_to.frame("iframe")
            driver.switch_to.frame(driver.find_element_by_xpath("//div[@class='ans-attach-ct']/iframe"))
            time.sleep(2)


        # 点击视频按钮
        try:
            driver.switch_to.default_content()
            video_elem.click()
            driver.switch_to.frame("iframe")
            driver.switch_to.frame(driver.find_element_by_xpath("//div[@class='ans-attach-ct']/iframe"))
            time.sleep(2)

            # 视频操作
            vedio = driver.find_element_by_id("video_html5_api")
            # driver.get(vedio.get_attribute("src"))

            # with open("test.html", "w", encoding="UTF8") as f:
            #    f.write(driver.page_source)
            # 开始播放
            driver.execute_script("return arguments[0].play()", vedio)
            time.sleep(5)
            #4倍速播放
            driver.execute_script("return arguments[0].playbackRate = 12", vedio)
            # 静音
            driver.execute_script("arguments[0].muted=true", vedio)
            # 检测视频是否播放完
            duration = driver.execute_script("return arguments[0].duration", vedio)
            print(duration/12)
            #time.sleep(duration/12)
            while True:
                time.sleep(5)
                try:
                    li_list=driver.find_elements_by_xpath("//li[@class='ans-videoquiz-opt']")
                    submit=driver.find_element_by_class_name("ans-videoquiz-submit")
                    #if driver.execute_script("return arguments[0].paused", vedio):
                    #    driver.execute_script("return arguments[0].play()", vedio)
                    #    driver.execute_script("return arguments[0].playbackRate = 12", vedio)
                except:
                    ended = driver.execute_script("return arguments[0].ended", vedio)
                    #print(ended)
                    if ended:
                        break
                else:#有题目出现
                    for li in li_list:
                        li.find_element_by_xpath(".//input").click()
                        submit.click()
                        try:
                            driver.find_elements_by_xpath("//li[@class='ans-videoquiz-opt']")
                        except:
                            break

        except Exception as e:
            pass
            # driver.execute_script("alert('视频播放出错!')")
            print(str(e))

        time.sleep(3)
        #回到课堂
        driver.switch_to.default_content()
        driver.find_element_by_xpath("//div[@class='goback']/a").click()


    time.sleep(2)
    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    frame = driver.switch_to.frame("frame_content")
    #with open("test.html", "w", encoding="UTF8") as f:
    #    f.write(driver.page_source)
